package com.xx.pmis.web.actions;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.Digest;
import com.xx.pmis.services.DigestService;
import com.xx.pmis.web.forms.DigestForm;
import com.xx.pmis.web.util.CurrentDate;

/**
 * @class 读书文摘管理Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class DigestAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(DigestAction.class);

	DigestService digestService = getPMISServices().getDigestService();

	// 查询所有读书文摘
	public ActionForward listDigest(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<?> all = null;
		try {
			all = digestService.getAllDigest();
		} catch (Exception e) {
			String errorPage = "【文档管理/读书文摘】查询【读书文摘】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("digestList", all);
		request.setAttribute("position", "文档管理/读书文摘");
		return mapping.findForward("listDigest");
	}

	// 新增读书文摘
	public ActionForward addDigest(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("position", "读书文摘/新增");
		return mapping.findForward("addDigest");
	}

	// 新增，处理方法
	public ActionForward addDigest_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		DigestForm f = (DigestForm) form;
		String bookTitle = f.getBookTitle(); // 书/电影名称
		String writer = f.getWriter(); // 作者/导演
		String digestText = f.getDigestText(); // 正文
		String digestType = f.getDigestType();// 类型，0 诗歌散文 1 影评书评 2 长篇小说
		String digestDesc = f.getDigestDesc();// 关系人/作品

		Digest digest = new Digest();
		digest.setEditTime(CurrentDate.currentDateAndTime());
		digest.setDigestDate(CurrentDate.currentDate());
		digest.setBookTitle(bookTitle);
		digest.setWriter(writer);
		digest.setDigestText(digestText);
		digest.setDigestType(digestType);
		digest.setDigestDesc(digestDesc);
		digest.setEditNum("1");

		try {
			digestService.saveOrUpdateDigest(digest);
		} catch (Exception e) {
			String errorPage = "【文档管理/读书文摘/新增】保存或更新【读书文摘】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		listDigestByPage(mapping, form, request, response);
		return mapping.findForward("listDigest");
	}

	// 编辑
	public ActionForward editDigest(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String digestID = request.getParameter("digestID");

		Digest digest = null;
		try {
			digest = digestService.getDigestByDigestID(digestID);
		} catch (Exception e) {
			String errorPage = "【文档管理/读书文摘/编辑】根据【读书文摘ID】查询该【读书文摘】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("digest", digest);
		request.setAttribute("position", "读书文摘/编辑");
		return mapping.findForward("editDigest");
	}

	// 编辑，处理方法
	public ActionForward editDigest_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		DigestForm f = (DigestForm) form;
		String digestID = f.getDigestID();
		String bookTitle = f.getBookTitle(); // 书/电影名称
		String writer = f.getWriter(); // 作者/导演
		String digestText = f.getDigestText(); // 正文
		String digestType = f.getDigestType();// 类型，0 诗歌散文 1 影评书评 2 长篇小说
		String digestDesc = f.getDigestDesc();// 关系人/作品

		Digest digest = null;
		try {
			digest = digestService.getDigestByDigestID(digestID);
		} catch (Exception e) {
			String errorPage = "【文档管理/读书文摘/编辑】根据【读书文摘ID】查询该【读书文摘】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		digest.setEditTime(CurrentDate.currentDateAndTime());
		digest.setBookTitle(bookTitle);
		digest.setWriter(writer);
		digest.setDigestText(digestText);
		digest.setDigestType(digestType);
		digest.setDigestDesc(digestDesc);
		digest.setEditNum(Integer.toString(Integer.parseInt(digest.getEditNum()) + 1));

		try {
			digestService.saveOrUpdateDigest(digest);
		} catch (Exception e) {
			String errorPage = "【文档管理/读书文摘/编辑】保存或更新【读书文摘】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		listDigestByPage(mapping, form, request, response);
		return mapping.findForward("listDigest");
	}

	// 删除
	public ActionForward delDigest(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Digest digest = null;
		DigestForm f = (DigestForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				digest = digestService.getDigestByDigestID(ids[i]);
			} catch (Exception e) {
				String errorPage = "【文档管理/读书文摘/删除】根据【读书文摘ID】查询该【读书文摘】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
			try {
				digestService.deleteDigest(digest);
			} catch (Exception e) {
				String errorPage = "【文档管理/读书文摘/删除】保存或更新【读书文摘】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
		}

		listDigestByPage(mapping, form, request, response);
		return mapping.findForward("listDigest");
	}

	// 明细
	public ActionForward detailDigest(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String digestID = request.getParameter("digestID");

		Digest digest = null;
		try {
			digest = digestService.getDigestByDigestID(digestID);
		} catch (Exception e) {
			String errorPage = "【文档管理/读书文摘/明细】根据【读书文摘ID】查询该【读书文摘】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("digest", digest);
		request.setAttribute("position", "读书文摘/明细");
		return mapping.findForward("detailDigest");
	}

	// 分页显示
	public ActionForward listDigestByPage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		int PAGESIZE = 20;// 每页记录数
		int totalPage = 1;// 总页数

		// 查询总用户记录数
		int count = 0;
		try {
			count = digestService.getCountOfDigest();
		} catch (Exception e) {
			String errorPage = "【文档管理/读书文摘】查询【读书文摘】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("count", count);
		if (count % PAGESIZE == 0) {
			totalPage = count / PAGESIZE;
		} else {
			totalPage = count / PAGESIZE + 1;
		}

		request.setAttribute("totalPage", totalPage);
		// 声明要翻的页
		String pageNum = "1";
		try {
			String thisPageNum = request.getParameter("pageNum");
			// 如果传了页码参数
			if (thisPageNum != null && thisPageNum.length() != 0) {
				pageNum = thisPageNum;
			}
		} catch (RuntimeException e) {
			log.info("" + e.toString());
		}
		request.setAttribute("pageNum", pageNum);
		List<?> all = null;
		try {
			all = digestService.getAllDigestByPage(Integer.parseInt(pageNum), PAGESIZE);
		} catch (Exception e) {
			String errorPage = "【文档管理/读书文摘】查询【读书文摘】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("digestList", all);
		request.setAttribute("PAGESIZE", PAGESIZE);
		// 如果要翻的这页是第一页
		if ("1".equals(pageNum)) {
			// 设置noUp为yes(界面上用此控制'首页'与'上一页'按钮失效)
			request.setAttribute("noUp", "yes");
		}

		// 如果要翻的这页是最后页
		if (totalPage == Integer.parseInt(pageNum)) {
			// 设置noDown为yes(界面上用此控制'下一页'与'尾页'按钮失效)
			request.setAttribute("noDown", "yes");
		}
		
		if(totalPage == 0) {
			request.setAttribute("pageNum", "0");
			request.setAttribute("noUp", "yes");
			request.setAttribute("noDown", "yes");
		}

		request.setAttribute("action", "digest.do");
		request.setAttribute("position", "文档管理/读书文摘");
		return mapping.findForward("listDigest");
	}
}